package com.tianqu.tms.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.webservice.weichat.api.impl.CollectCarInfoImpl;

/**
 * dao的基类
 * 
 * @author liugang
 * 
 */

public abstract class TmsDao {

	protected Connection conn = null;
	protected Logger logger = Logger.getLogger(TmsDao.class);
	protected static final String ERROR_ID = TmsException.DB_TXCRROR;
	/*
	 * TMS 数据库表名key的定义
	 */
	// tis user dao
	public static final int TMS_USER = 1;
	// tis car_info dao
	public static final int TMS_CAR = 2;
	// tis detect_normal dao
	public static final int VIEWLIST = 3;
	// tis detect_senior dao
	public static final int TMS_MEMBER = 4;
	// tis detect_pic dao
	public static final int TMS_PIC = 5;
	// tms codes
	public static final int TMS_CODES = 6;
	//
	public static final int TMS_SEQUENCE = 7;

	// tms_vendor
	public static final int TMS_VENDOR = 8;

	// tms_vehicle_brand
	public static final int TMS_VEHICLE_BRNAD = 9;
	// tms_car_style
	public static final int TMS_CAR_STYLE = 10;
	// tms_user_function
	public static final int TMS_USER_FUNCTION = 11;
	// tms_user_auth
	public static final int TMS_USER_AUTH = 12;
	// TMS_USER_CLASS
	public static final int TMS_USER_CLASS = 13;

	public static final int TMS_BASE_CAR_INFO = 14;

	public static final int TMS_AUC_BIDTYPE = 15;

//	public static final int TMS_AUC_MANAGER = 16;
	public static final int TMS_AUC_MANAGEMENT = 16;
	// tms_auc_bidhistory
	public static final int TMS_AUC_BIDHISTORY = 17;

	public static final int TMS_USER_ROLE = 18;

	//
	public static final int TMS_DETECT_NORMAL = 19;

	public static final int TMS_DETECT_SENIOR = 20;

	// 检测图片
	public static final int TMS_DETECT_PIC = 21;
	// 检测车辆基本信息表
	public static final int TMS_DECT_CAR_INFO = 22;
	// 检测字典表
	public static final int DECT_CODE = 23;
	// 左侧菜单栏字典表
	public static final int TMS_FUNCTION_CODES = 24;
	// 会员车辆统计视图
	public static final int TMS_CARPORTCOUNT_VIEW = 25;
	// 车辆统计表
	public static final int TMS_MEMBER_INVENTORY = 26;
	//
	public static final int TMS_CHECK_PAD_PIC = 27;
	//
	public static final int TMS_MARKET_PAD_PIC = 28;
	// 待办车辆基本信息表
	public static final int TMS_PROXY_CAR_INFO = 29;
	// 查验车辆基本信息表
	public static final int TMS_CHECK_CAR_INFO = 30;
	// 查验车辆基本信息表
	public static final int TMS_CHECK_INFO_PIC = 31;
	// 待办资料图片表
	public static final int TMS_PROXY_PIC = 32;
	// 评估
	public static final int TMS_ASSESS_CAR_INFO = 33;
	// 待办缴费
	public static final int TMS_PROXY_CAR_PAYMENT = 34;
	//过业务基本信息表
	public static final int TMS_TRANSFER_ACCEPTED_INFO = 35;
	//非库存车辆基本信息
	public static final int TMS_TRANSFER_PERSONALCAR_INFO = 36;
	//过户财务收费表
	public static final int TMS_TRANSFER_FINANCE_INFO = 37;
	//过户财务收费表
	public static final int TMS_TRANSFER_PROCESS_HISTORY = 38;
	//库存车辆进场库记录信息表
	public static final int TMS_MARKET_CAR_GATEINFO = 39;
	//经办人
	public static final int TMS_ATTN = 40;
	public static final int TMS_TRANSMARKET_CODE=41;
	public static final int TMS_INTEGRAL_REGISTER=42;
	//积分卡变更记录表
	public static final int TMS_INTEGRALCARD_UPDATE_HISTORY = 43;
	//登记信息变更表
	public static final int TMS_CHANGE = 44;	
	// tms_commercial_vehicle
	public static final int TMS_COMMERCIAL_VEHICLE = 45;
	public static final int TMS_SMS_INFO = 46;
	public static final int TMS_TRANSFER_SETTLEMENT_DATE = 47;
	public static final int TMS_TRANSFER_SETTLEMENT_INFO = 48;
	public static final int TMS_TRANSFER_SETTLEMENT_DAY = 49;
	public static final int TMS_TRANSFER_MONTH_REPORTFORM = 50;
	public static final int TMS_TRANSFER_TICKETD_INFO = 73;
	
	public static final int TMS_TRANSFER_SPECIALCAR_COUNT = 51;
	public static final int TMS_TRANSFER_WORK_COUNT = 52;
	public static final int TMS_TRANSFER_STATUS_COUNT = 53;
	public static final int TMS_MARKET_CAR_TRANS_INFO = 54;
	public static final int TMS_API_LOG = 55;
	public static final int TMS_CONFIGURE = 56;
	public static final int TMS_CAR_MODEL = 57;
	public static final int TMS_CAR_MODEL_VIN = 58;
	public static final int TMS_FRAGILESTICKER_HISTORY = 59;
	public static final int TMS_RENTALLOCATION_INFO = 60;
	public static final int TMS_CONTRACT_INFO = 61;
	public static final int TMS_MARKET_CAR_HANDLEINFO = 62;
	public static final int TMSAPP_MARKET_CAR_INFO_TEMP = 63;
	public static final int TMS_MARKET_CAR_INFO = 64;
	public static final int TMS_MARKET_CAR_SERVICE = 65;
	public static final int TMS_MESSAGE_HISTORY = 66;
	public static final int TMS_MARKET_CAR_SERVICE_INFO = 67;
	//过户业务记录表
	public static final int TMS_TRANSFER_ACCEPTED_HISTORY = 68;
    // 车辆养护信息表
    public static final int TMS_CONSERVE_INFO = 69;

    // 过户业务基本收费表
    public static final int TMS_TRANSFER_FINANCE_DETAIL = 70; 

    public static final int TMS_CHECK_USER = 71;
    
    // 变更表打印记录表 
    public static final int TMS_INFOCHANGE_HISTORY = 72;
    // 牌证表打印记录表 
    public static final int TMS_PAIZHENG_HISTORY = 75;
    // 车管所
    public static final int TMS_CITY = 74;

    public static final int TMS_FINANCIA = 76;
    
    public static final int TMS_MARKET_CAR_FINANCIAL = 77;
    
    public static final int MarketCarVendorCount = 78;
	/**
	 * 
	 * @throws TmsDaoException
	 */
	protected TmsDao() throws TmsDaoException {
		try {
			setConnection();
		} catch (TmsDaoException e) {
			throw new TmsDaoException(TmsException.DB_TXCRROR, e);
		}
	}

	protected void setConnection() throws TmsDaoException {
		Connection conn;
		try {
			conn = TxController.getTxController().getConnection();
		} catch (TransactionException e) {
			logger.error("Conection获得失败" + e);
			throw new TmsDaoException(TmsException.DB_TXCRROR);
		}
		this.conn = conn;
	}

	/**
	 * 
	 * @param daoId
	 * @return
	 * @throws TmsDaoException
	 */
	public static TmsDao getInstance(int daoId) throws TmsDaoException {
		switch (daoId) {
		case MarketCarVendorCount:
			return new MarketCarVendorCountDaoImpl();
		case TMS_USER:
			return new UserDaoImp();
		case TMS_CAR:
			return new CarDaoImp();
		case TMS_SEQUENCE:
			return new SequenceDaoImp();
		case TMS_MEMBER:
			return new MemberDaoImp();
		case TMS_PIC:
			return new CarImageDaoImp();
		case TMS_CODES:
			return new CodesDaoImp();
		case TMS_VENDOR:
			return new VendorDaoImp();
		case TMS_VEHICLE_BRNAD:
			return new VehicleBrandDaoImp();
		case TMS_CAR_STYLE:
			return new CarStyleDaoImp();
		case TMS_USER_AUTH:
			return new UserAuthDaoImp();
		case TMS_USER_CLASS:
			return new UserClassDaoImp();
		case TMS_BASE_CAR_INFO:
			return new AucCarInfoDaoImp();
		case TMS_AUC_BIDTYPE:
			return new AucBidTypeDaoImp();
		case TMS_AUC_MANAGEMENT:
			return new AucManagerDaoImp();
		case TMS_AUC_BIDHISTORY:
			return new AucBidhistoryDaoImp();
		case TMS_USER_ROLE:
			return new UserRoleDaoImp();
		case TMS_DETECT_NORMAL:
			return new TmsDetectNormalDaoImp();
		case TMS_DETECT_SENIOR:
			return new TmsDetectSeniorDaoImp();
		case TMS_DETECT_PIC:
			return new TmsDetectPicDaoImp();
		case TMS_DECT_CAR_INFO:
			return new TmsDetectInfoImp();
		case DECT_CODE:
			return new DectCodesDaoImp();
		case TMS_CARPORTCOUNT_VIEW:
			return new CarPortCountDaoImp();
		case TMS_CHECK_PAD_PIC:
			return new CheckPadPicDaoImp();
		case TMS_MARKET_PAD_PIC:
			return new MarketPadPicDapImp();
		case TMS_PROXY_CAR_INFO:
			return new ProxyCarInfoDaoImp();
		case TMS_CHECK_CAR_INFO:
			return new CheckInfoDaoImp();
		case TMS_CHECK_INFO_PIC:
			return new CheckInfoPicDaoImp();
		case TMS_PROXY_PIC:
			return new TmsProxyPicDaoImp();
		case TMS_ASSESS_CAR_INFO:
			return new AssessCarInfoDaoImp();
		case TMS_PROXY_CAR_PAYMENT:
			return new ProxyCarPaymentDaoImp();
		case TMS_TRANSFER_ACCEPTED_INFO:
			return new TransferAcceptedInfoDaoImp();
		case TMS_TRANSFER_PERSONALCAR_INFO:
			return new TransferPersonalCarInfoDaoImp();
		case TMS_TRANSFER_FINANCE_INFO:
			return new TransferFinanceInfoDaoImp();
		case TMS_TRANSFER_PROCESS_HISTORY:
			return new TransferProcessHistoryDaoImp();
		case TMS_MARKET_CAR_GATEINFO:
			return new MarketCarGateInfoImp();
		case TMS_ATTN:
			return new AttnDaoImp();
		case TMS_TRANSMARKET_CODE:
			return new TransMarketCodeDaoImp();
		case TMS_INTEGRAL_REGISTER:
			return new IntegralRegisterDaoImp();
		case TMS_INTEGRALCARD_UPDATE_HISTORY:
			return new IntegralcardUpdateHistoryDaoImp();	
		case TMS_CHANGE:
			return new TmsChangeDaoImp();
		case TMS_COMMERCIAL_VEHICLE:
			return new CommercialVehicleDaoImp();
		case TMS_SMS_INFO:
			return new TmsSmsInfoDaoImp();
		case TMS_TRANSFER_SETTLEMENT_DATE:
			return new TransferSettlementDateDaoImp();
		case TMS_TRANSFER_TICKETD_INFO:
		    return new TransferTicketdInfoDaoImp();
		case TMS_TRANSFER_SETTLEMENT_INFO:
			return new TransferSettlementInfoDaoImp();
		case TMS_TRANSFER_SETTLEMENT_DAY:
			return new TransferSettlementDayDaoImp();
		case TMS_TRANSFER_MONTH_REPORTFORM:
			return new TransferMonthReportFormDaoImp();
		case TMS_TRANSFER_SPECIALCAR_COUNT:
			return new TransferSpecialCarCountDaoImp();
		case TMS_TRANSFER_WORK_COUNT:
			return new TransferWorkCountDaoImp();
		case TMS_TRANSFER_STATUS_COUNT:
			return new TransferStatusCountDaoImp();
		case TMS_MARKET_CAR_TRANS_INFO:
			return new MarketCarTranserInfoDaoImp();
		case TMS_API_LOG:
			return new TmsApiLogDaoImp();
		case TMS_CONFIGURE:
			return new TmsConfigureDaoimp();
		case TMS_CAR_MODEL:
			return new TmsCarModelDaoImp();
		case TMS_CAR_MODEL_VIN:
			return new TmsCarmodelVinDaoImp();
		case TMS_FRAGILESTICKER_HISTORY:
			return new TmsFragileStickerHistoryDaoImp();
		case TMS_RENTALLOCATION_INFO:
			return new RentalLocationInfoDaoImp();
		case TMS_CONTRACT_INFO:
			return new RentalContractDaoImp();
		case TMS_MARKET_CAR_HANDLEINFO:
			return new MarketCarHandleInfoDaoImp();
		case TMSAPP_MARKET_CAR_INFO_TEMP:
			return new MarketCarInfoTempDaoImp();
		case TMS_MARKET_CAR_INFO:
			return new CollectCarInfoImpl();
		case TMS_MARKET_CAR_SERVICE:
			return new CollectCarInfoImpl();
		case TMS_MESSAGE_HISTORY:
			return new TmsMessageHistoryDaoImp();
		case TMS_MARKET_CAR_SERVICE_INFO:
			return new MarketCarServiceDaoImp();
		case TMS_TRANSFER_ACCEPTED_HISTORY:
			return new TransferAcceptedHistoryDaoImp();
		case TMS_CONSERVE_INFO:
		    return new ConserveInfoDaoImp();
		case TMS_TRANSFER_FINANCE_DETAIL:
            return new TmsTransferFinanceDetailDaoImp();
//		    return new FinanceDetailDaoImp();
		case TMS_CHECK_USER:
			return new TmsCheckUserDaoImp();
		case TMS_PAIZHENG_HISTORY:
		    return new PaizhengHistoryDaoImp();
		case TMS_INFOCHANGE_HISTORY:
		    return new InfoChangeHistoryDaoImp();
		case TMS_CITY:
		    return new CityDaoImp();
		case TMS_FINANCIA:
		    return new TmsFinanciaDaoImp();
		case TMS_MARKET_CAR_FINANCIAL:
		    return new TmsMarketCarFinancialDaoImp();
		default:
			return null;
		}
	}

	public static ViewListDao getViewListDao(TmsDao dao, int daoId) {
		switch (daoId) {
		case TMS_CAR:
			return (CarDaoImp) dao;
		case TMS_AUC_MANAGEMENT:
			return (AucManagerDaoImp) dao;
		case TMS_BASE_CAR_INFO:
			return (AucCarInfoDaoImp) dao;
		case TMS_MEMBER:
			return (MemberDaoImp) dao;
		case TMS_DECT_CAR_INFO:
			return (TmsDetectInfoImp) dao;
		case TMS_MEMBER_INVENTORY:
			return (CarPortCountDaoImp) dao;
		case TMS_PROXY_CAR_INFO:
			return (ProxyCarInfoDaoImp) dao;
		case TMS_TRANSFER_ACCEPTED_INFO:
			return (TransferAcceptedInfoDaoImp) dao;	
		case TMS_ATTN:
			return (AttnDaoImp) dao;
		case TMS_USER:
			return (UserDaoImp) dao;
		case TMS_RENTALLOCATION_INFO:
			return (RentalLocationInfoDaoImp) dao;
		case TMS_CONTRACT_INFO:
			return (RentalContractDaoImp) dao;
		case TMSAPP_MARKET_CAR_INFO_TEMP:
			return (MarketCarInfoTempDaoImp) dao; 
		case TMS_FINANCIA:
		    return (TmsFinanciaDaoImp) dao;
		case TMS_MARKET_CAR_FINANCIAL:
			 return (TmsMarketCarFinancialDaoImp) dao;
		default:
			return null;
		}
	}

	/**
	 * @param daoId
	 * @param pageNo
	 * @param maxIndex
	 * @param sortKey
	 * @param asc
	 * @return
	 * @throws TmsDaoException
	 */
	public static TmsDao getInstance(int daoId, int pageNo, int maxIndex,
			int sortKey, boolean asc) throws TmsDaoException {
		switch (daoId) {
		case TMS_CAR:
			return new CarDaoImp(pageNo, maxIndex, sortKey, asc);
		case TMS_AUC_MANAGEMENT:
			return new AucManagerDaoImp(pageNo, maxIndex, sortKey, asc);
		case TMS_BASE_CAR_INFO:
			return new AucCarInfoDaoImp(pageNo, maxIndex, sortKey, asc);
		case TMS_MEMBER:
			return new MemberDaoImp(pageNo, maxIndex, sortKey, asc);
		case TMS_DECT_CAR_INFO:
			return new TmsDetectInfoImp(pageNo, maxIndex, sortKey, asc);
		case TMS_MEMBER_INVENTORY:
			return new CarPortCountDaoImp(pageNo, maxIndex, sortKey, asc);
		case TMS_PROXY_CAR_INFO:
			return new ProxyCarInfoDaoImp(pageNo, maxIndex, sortKey, asc);
		case TMS_TRANSFER_ACCEPTED_INFO:
			return new TransferAcceptedInfoDaoImp(pageNo, maxIndex, sortKey, asc);	
		case TMS_ATTN:
			return new AttnDaoImp(pageNo, maxIndex, sortKey, asc);	
		case TMS_USER:
			return new UserDaoImp(pageNo, maxIndex, sortKey, asc);
		case TMS_RENTALLOCATION_INFO:
			return new RentalLocationInfoDaoImp(pageNo, maxIndex, sortKey, asc);
		case TMS_CONTRACT_INFO:
			return new RentalContractDaoImp(pageNo, maxIndex, sortKey, asc);
		case TMSAPP_MARKET_CAR_INFO_TEMP:
			return new MarketCarInfoTempDaoImp(pageNo, maxIndex, sortKey, asc);
		case TMS_FINANCIA:
		    return new TmsFinanciaDaoImp(pageNo, maxIndex, sortKey, asc);
		case TMS_MARKET_CAR_FINANCIAL:
		    return new TmsMarketCarFinancialDaoImp(pageNo, maxIndex, sortKey, asc);
		default:
			return null;
		}
	}

	/**
	 * 
	 * @return
	 */
	protected Connection getConnection() {
		return conn;
	}

	/**
	 * Closes java.sql.ResultSet object if it is not null.
	 * 
	 * @param rs
	 * @throws TmsDaoException
	 */
	protected void close(ResultSet rs) throws TmsDaoException {
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			throw new TmsDaoException(TmsException.DB_TXCRROR, e);
		}
	}

	/**
	 * Closes java.sql.Statement object if it is not null.
	 * 
	 * @param stmt
	 * @throws TmsDaoException
	 */
	protected void close(Statement stmt) throws TmsDaoException {
		try {
			if (stmt != null) {
				stmt.close();
			}
		} catch (SQLException e) {
			throw new TmsDaoException(TmsException.DB_TXCRROR, e);
		}
	}

	/**
	 * Closes java.sql.Connection object if it is not null.
	 * 
	 * @param conn
	 * @throws TmsDaoException
	 */
	protected void close(Connection conn) throws TmsDaoException {
		try {
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
			throw new TmsDaoException(TmsException.DB_TXCRROR, e);
		}
	}

	/**
	 * Closes java.sql.ResultSet, java.sql.Statement, java.sql.Connection
	 * objects if they are not null.
	 * 
	 * @param rs
	 * @param stmt
	 * @param conn
	 * @throws TmsDaoException
	 */
	protected void close(ResultSet rs, Statement stmt, Connection conn)
			throws TmsDaoException {
		try {
			this.close(rs);
		} finally {
			try {
				this.close(stmt);
			} finally {
				this.close(conn);
			}
		}
	}

	/**
	 * Closes java.sql.ResultSet, java.sql.Statement
	 * 
	 * @param rs
	 * @param stmt
	 * @throws TmsDaoException
	 */
	protected void close(ResultSet rs, Statement stmt) throws TmsDaoException {
		try {
			this.close(rs);
		} finally {
			this.close(stmt);
		}
	}

	/**
	 * 
	 * @param date
	 * @return Date
	 */
	protected java.sql.Date toSqlDate(java.util.Date date) {
		if (date == null) {
			return null;
		}
		java.sql.Date sqlDate = new java.sql.Date(date.getTime());
		return sqlDate;
	}

	/**
	 * 
	 * @param date
	 * @return Timestamp
	 */
	protected java.sql.Timestamp toSqlTimestamp(java.util.Date date) {
		if (date == null) {
			return null;
		}
		java.sql.Timestamp sqlTs = new java.sql.Timestamp(date.getTime());
		return sqlTs;
	}

	/**
	 * 
	 * @param date
	 * @return Date
	 */
	protected java.util.Date toUtilDateFromSqlDate(java.sql.Date date) {
		if (date == null) {
			return null;
		}
		java.sql.Date sqlDate = new java.sql.Date(date.getTime());
		return sqlDate;
	}

	/**
	 * 
	 * @param date
	 * @return Date
	 */
	protected java.util.Date toUtilDateFromSqlTimestamp(java.sql.Timestamp date) {
		if (date == null) {
			return null;
		}
		java.sql.Date sqlDate = new java.sql.Date(date.getTime());
		return sqlDate;
	}

}
